package com.market.sys.api.client;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.market.sys.common.Result;
import com.market.sys.entity.UserCollection;
import com.market.sys.entity.UserLike;
import com.market.sys.service.IUserCollectionService;
import com.market.sys.util.TimeUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author xd
 * @since 2024-03-26
 */
@RestController
@RequestMapping("/sys/userCollection")
public class UserCollectionController {

    @Resource
    private IUserCollectionService userCollectionService;

    @PostMapping("/add")
    public Result<?> addLike(@RequestBody Map<String,Object> paramsCondition){
        UserCollection userCollection = BeanUtil.fillBeanWithMap(paramsCondition, new UserCollection(), false);
        userCollection.setCollectTime(TimeUtil.timeYmdhms(new Date()));
        if (userCollectionService.save(userCollection)){
            return Result.success("收藏成功");
        }else {
            return Result.error("收藏失败");
        }
    }

    @PostMapping("/cancel")
    public Result<?> cancelLike(@RequestBody Map<String,Object> paramsCondition){
        UserCollection userCollection = BeanUtil.fillBeanWithMap(paramsCondition, new UserCollection(), false);
        if (userCollectionService.remove(new LambdaQueryWrapper<UserCollection>().eq(UserCollection::getUserId,userCollection.getUserId())
                .eq(UserCollection::getThingId,userCollection.getThingId()).eq(UserCollection::getType,userCollection.getType()))){
            return Result.success("取消点赞成功");
        }else {
            return Result.error("取消点赞失败");
        }
    }

    @PostMapping("/isCollect")
    public Result<?> isLike(@RequestBody Map<String,Object> paramsCondition){
        UserCollection userCollection = BeanUtil.fillBeanWithMap(paramsCondition, new UserCollection(), false);
        if (userCollectionService
                .query()
                .eq("thing_id",userCollection.getThingId())
                .eq("user_id",userCollection.getUserId())
                .eq("type",userCollection.getType()).one()
                != null
        ){
            return Result.success();
        }else {
            return Result.error();
        }
    }
}
